package com.njwb.Swing.Supplier;

import com.njwb.Util.UtilService;
import com.njwb.pojo.Supplier;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class SupplierController {
    private SupplierView view;
    private UtilService utilService;

    public SupplierController(SupplierView view, UtilService utilService) {
        this.view = view;
        this.utilService = utilService;

        // 初始化数据
        loadData();

        // 绑定事件
        bindEvents();
    }

    private void bindEvents() {
        view.searchButton.addActionListener(e -> {
            String name = view.searchField.getText().trim();
            if (name.isEmpty()) loadData();
            else loadDataByName(name);
        });

        view.addButton.addActionListener(e -> {
            SupplierDialog dialog = new SupplierDialog(
                    (Frame) SwingUtilities.getWindowAncestor(view),
                    "新增供应商", null, success -> { if(success) loadData(); }
            );
            dialog.setVisible(true);
        });

        view.editButton.addActionListener(e -> {
            int selectedRow = view.supplierTable.getSelectedRow();
            if(selectedRow == -1){
                JOptionPane.showMessageDialog(view, "请先选择要修改的供应商！");
                return;
            }
            Supplier supplier = new Supplier(
                    (int) view.supplierTableModel.getValueAt(selectedRow,0),
                    (String) view.supplierTableModel.getValueAt(selectedRow,1),
                    (String) view.supplierTableModel.getValueAt(selectedRow,2),
                    (String) view.supplierTableModel.getValueAt(selectedRow,3),
                    (String) view.supplierTableModel.getValueAt(selectedRow,4),
                    (String) view.supplierTableModel.getValueAt(selectedRow,5)
            );
            SupplierDialog dialog = new SupplierDialog(
                    (Frame) SwingUtilities.getWindowAncestor(view),
                    "修改供应商", supplier, success -> { if(success) loadData(); }
            );
            dialog.setVisible(true);
        });

        view.deleteButton.addActionListener(e -> {
            int[] selectedRows = view.supplierTable.getSelectedRows();
            if(selectedRows.length==0){
                JOptionPane.showMessageDialog(view, "请先选择要删除的供应商！");
                return;
            }
            StringBuilder ids = new StringBuilder();
            for(int row:selectedRows){
                ids.append(view.supplierTableModel.getValueAt(row,0)).append(", ");
            }
            String idStr = ids.substring(0, ids.length()-2);

            int confirm = JOptionPane.showConfirmDialog(view,"确定删除以下供应商吗？\n"+idStr,"确认删除",JOptionPane.YES_NO_OPTION);
            if(confirm==JOptionPane.YES_OPTION){
                boolean allSuccess=true;
                for(int row:selectedRows){
                    int id=(int)view.supplierTableModel.getValueAt(row,0);
                    if(!utilService.deleteSupplier(id)) allSuccess=false;
                }
                JOptionPane.showMessageDialog(view, allSuccess?"删除成功！":"部分供应商删除失败！",
                        allSuccess? "提示": "警告",
                        allSuccess? JOptionPane.INFORMATION_MESSAGE:JOptionPane.WARNING_MESSAGE);
                loadData();
            }
        });

        view.importButton.addActionListener(e -> {
            utilService.importSupplier(view);
            loadData();
        });
    }

    public void loadData() {
        ArrayList<Supplier> list = utilService.getAllSupplier();
        view.supplierTableModel.setRowCount(0);
        for(Supplier s:list){
            view.supplierTableModel.addRow(new Object[]{
                    s.getSupplier_id(),
                    s.getName(),
                    s.getDescription(),
                    s.getContact_person(),
                    s.getPhone(),
                    s.getAddress()
            });
        }
    }

    public void loadDataByName(String name) {
        ArrayList<Supplier> list = utilService.getSupplierByName(name);
        view.supplierTableModel.setRowCount(0);
        for(Supplier s:list){
            view.supplierTableModel.addRow(new Object[]{
                    s.getSupplier_id(),
                    s.getName(),
                    s.getDescription(),
                    s.getContact_person(),
                    s.getPhone(),
                    s.getAddress()
            });
        }
    }
}
